#!/bin/bash



########################################
# all the config files for vim needs to be update-to-date 
# so everytime copy all the files from local config to 
# system config dir
########################################

if [[ -z "$LOCAL_CONFIG_HOME" ]]; then
    export LOCAL_VIM_HOME="$(pwd)"
else
    export LOCAL_VIM_HOME="$LOCAL_CONFIG_HOME/lib/vim"
fi

export LOCAL_VIM_CONFIG="$LOCAL_VIM_HOME/_vim/config"

export SYSTEM_VIME_HOME="$HOME/.vim"
export SYSTEM_VIM_CONFIG="$SYSTEM_VIME_HOME/config"




##################################################
# function used to init vim configurations
# 1. create the customize configuration dir if not exists
# 2. copy customized configuration files into the dir
##################################################
__init_vim_configuration() {

    __debug "initiate vim configurations"
    
    if [[ ! -d $SYSTEM_VIM_CONFIG  ]]; then
        mkdir $SYSTEM_VIM_CONFIG
    fi

    cp -r $LOCAL_VIM_CONFIG/* $SYSTEM_VIM_CONFIG
}


__create_local_vimrc() {

    __debug "creating self vimrc configuration file"
    
    rm $LOCAL_VIM_HOME/.vimrc
    touch $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/Vundle_Plugins.vim" >> $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/Common_config.vim" >> $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/FileType_Config.vim" >> $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/Function_Config.vim" >> $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/Mapping_Config.vim" >> $LOCAL_VIM_HOME/.vimrc
    echo "source ~/.vim/config/Vundle_Plugins_Cofig.vim" >> $LOCAL_VIM_HOME/.vimrc
   
    __debug "copy local vimrc to system "
    cp $LOCAL_VIM_HOME/.vimrc ~/.vimrc
}

__end_vim_configuration() {

    #create vim swp dir
    if [[ ! -d ~/.swp ]]; then
        mkdir ~/.swp
    fi

    # install  vim plugins
    vim +PluginInstall +qall

    # Config jedi-vim plugin 
    cd ~/.vim/bundle/jedi-vim
    git submodule update --init
    cd ~/gitworkspace/.dotfiles
}

__debug(){
    echo -e "\n####### $1 #####\n"
}



__init_vim_configuration 

source "$LOCAL_VIM_HOME/config/Vundle_Install.sh" 
source "$LOCAL_VIM_HOME/config/Color_Install.sh" 
source "$LOCAL_VIM_HOME/config/FileTypePlugin_Install.sh" 
source "$LOCAL_VIM_HOME/config/After_Filetype_Install.sh" 

__create_local_vimrc
__end_vim_configuration 




